package com.atguigu.zhxy.service.impl;

import com.atguigu.zhxy.pojo.Admin;
import com.atguigu.zhxy.pojo.Clazz;
import com.atguigu.zhxy.pojo.LoginForm;
import com.atguigu.zhxy.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.zhxy.pojo.Student;
import com.atguigu.zhxy.service.StudentService;
import com.atguigu.zhxy.mapper.StudentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* @author Hoker
* @description 针对表【tb_student】的数据库操作Service实现
* @createDate 2024-06-02 10:49:26
*/
@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Override
    public Student login(LoginForm loginForm) {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Student::getName, loginForm.getUsername())
                .eq(Student::getPassword, MD5.encrypt(loginForm.getPassword()));
        Student student = baseMapper.selectOne(lambdaQueryWrapper);
        return student;
    }

    @Override
    public IPage<Student> getClazzsByOpr(Page<Student> pageParam, Student student) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (student != null) {
            String gradeName = student.getClazzName();
            if (!StringUtils.isEmpty(gradeName)){
                queryWrapper.eq("clazz_name",gradeName);
            }
            String clazzName = student.getName();
            if (!StringUtils.isEmpty(clazzName)){
                queryWrapper.like("name",clazzName);
            }

            queryWrapper.orderByDesc("id");
            queryWrapper.orderByAsc("name");
        }
        Page page = baseMapper.selectPage(pageParam,queryWrapper);

        return page;
    }
}




